package contaKM2012;

import exceptions.InconsistentValueException;
import interfaces.IContaKm;

public class ContaKm implements IContaKm {
	protected int km;

	public ContaKm() throws InconsistentValueException {
		km = IContaKm.initVal;
		if (!invariant()) {
			throw new InconsistentValueException("km = " + km);
		}
	}

	public void inc() throws InconsistentValueException {
		if (km != IContaKm.limit) {
			km++;
		} else {
			km = 0;
		}
		if (!invariant()) {
			throw new InconsistentValueException("km = " + km);
		}
	}

	public int getVal() {
		return km;
	}

	protected boolean invariant() {
		return km >= IContaKm.initVal && km <= IContaKm.limit;
	}
}
